package io.modifier.basic.compose

import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.wquick.base.R
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.emo.ui.core.TopBar
import io.modifier.basic.emo.ui.core.TopBarBackIconItem
import io.modifier.basic.emo.ui.core.TopBarItem
import io.modifier.basic.emo.ui.core.TopBarWithLazyListScrollState
import io.modifier.basic.emo.ui.core.modifier.windowInsetsCommonHorPadding
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf


@Composable
fun SimpleBackPage(
    title: String = " ",
    topBarRightItems: PersistentList<TopBarItem> = persistentListOf(),
    blackTint: Boolean = true,
    bgColor: Color = AppThemeHolder.colors.bgColor,
    topBarBg: Color = bgColor,
    titleColor: Color = AppThemeHolder.colors.onAppColor,
    contentPaddingValues: PaddingValues = PaddingValues(top = 8.dp),
    loadingWatcher: Boolean = false,
    content: @Composable BoxScope .() -> Unit,
) {
    val backHandler = LocalOnBackPressedDispatcherOwner.current
    Column(
        modifier = Modifier
            .fillMaxSize(),
    ) {

        TopBar(
            title = { title },
            titleColor = if (blackTint) AppThemeHolder.colors.txtColor else titleColor,
            leftItems = persistentListOf(
                TopBarBackIconItem(
                    returnIcon = if (blackTint) R.drawable.svg_return_black else R.drawable.svg_return_black,
                    tint = if (blackTint) Color.Black else Color.White
                ) {
                    backHandler?.onBackPressedDispatcher?.onBackPressed()
                },
            ),
            rightItems = topBarRightItems,
            backgroundColor = topBarBg,
        )

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .background(bgColor)
                .windowInsetsCommonHorPadding()
                .padding(contentPaddingValues),
            content = {
                content()
            },
        )
    }

    if (loadingWatcher) LoadingWatcher()
}


@Composable
fun SimpleListPage(
    title: String = " ",
    blackTint: Boolean = true,
    bgColor: Color = AppThemeHolder.colors.bgColor,
    topBarBg: Color = bgColor,
    titleColor: Color = if (blackTint) AppThemeHolder.colors.txtColor else AppThemeHolder.colors.onAppColor,
    topBarRightItems: PersistentList<TopBarItem> = persistentListOf(),
    contentPaddingValues: PaddingValues = PaddingValues(top = 8.dp),
    content: LazyListScope .() -> Unit,
) {
    val backHandler = LocalOnBackPressedDispatcherOwner.current
    Column(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        val scrollState = rememberLazyListState()
        TopBarWithLazyListScrollState(
            scrollState,
            backgroundColor = topBarBg,
            titleColor = titleColor,
            title = { title },
            leftItems = persistentListOf(
                TopBarBackIconItem(
                    returnIcon = if (blackTint) R.drawable.svg_return_black else R.drawable.svg_return_black,
                    tint = if (blackTint) Color.Black else Color.White
                ) {
                    backHandler?.onBackPressedDispatcher?.onBackPressed()
                },
            ),
            rightItems = topBarRightItems,
        )
        LazyColumn(
            state = scrollState,
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .background(bgColor)
                .windowInsetsCommonHorPadding(),
            contentPadding = contentPaddingValues,
            content = {
                content()
            },
        )
    }
}


@Composable
fun ScreenPage(
    title: String = " ",
    topBarLeftItems: PersistentList<TopBarItem> = persistentListOf(),
    topBarRightItems: PersistentList<TopBarItem> = persistentListOf(),
    blackTint: Boolean = true,
    bgColor: Color = AppThemeHolder.colors.bgColor,
    topBarBg: Color = bgColor,
    titleColor: Color = AppThemeHolder.colors.onAppColor,
    contentPaddingValues: PaddingValues = PaddingValues(top = 8.dp),
    loadingWatcher: Boolean = false,
    content: @Composable BoxScope .() -> Unit,
) {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(bgColor),
    ) {

        TopBar(
            title = { title },
            titleColor = if (blackTint) AppThemeHolder.colors.txtColor else titleColor,
            leftItems = topBarLeftItems,
            rightItems = topBarRightItems,
            backgroundColor = topBarBg,
            shadowAlpha = { 0f },
            separatorHeight = 0.dp
        )

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .windowInsetsCommonHorPadding()
                .padding(contentPaddingValues),
            content = {
                content()
            },
        )
    }

    if (loadingWatcher) LoadingWatcher()
}